package odgs;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import odgs.listener.ExplorerNodeModifyListener;
import odgs.manager.filedb.FileDBContents;

public class Contents implements ExplorerNode, Serializable
{
	private int uid;
	private String name;
	private String path;
	private String owner;
	private String type;
	private long size;
	private int permission;
	transient private List<ExplorerNodeModifyListener> explorerNodeListeners =  new ArrayList<ExplorerNodeModifyListener>();
	
	public Contents(String name, String path, String type)
	{
		this.name = name;
		this.path = path;
		this.type = type;
	}

	public Contents(String name, String type)
	{
		this(name, "", type);
	}

	public Contents(int uid, String name, String type, String path, String owner, long size, int permission)
	{
		this.uid = uid;
		this.name = name;
		this.type = type;
		this.path = path;
		this.owner = owner;
		this.size = size;
		this.permission = permission;
	}
	
	public Contents(int uid2)
	{
		uid = uid2;
	}

	public boolean canChildren()
	{
		return false;
	}

	public Contents clones()
	{
		Contents con = new Contents(name, type);
		con.setOwner(owner);
		con.setPath(path);
		con.setPermission(permission);
		con.setSize(size);
		con.setExplorerNodeListener(explorerNodeListeners);
		
		return con;
	}

	private void setExplorerNodeListener(List<ExplorerNodeModifyListener> explorerNodeListeners)
	{
		this.explorerNodeListeners = explorerNodeListeners;
	}

	public String getName()
	{
		return name;
	}

	public String getOwner()
	{
		return owner;
	}

	public String getPath()
	{
		return path;
	}

	public int getPermission()
	{
		return permission;
	}

	public int getNodeSize()
	{
		return 0;
	}
	
	public long getSize()
	{
		return size;
	}

	public String getType()
	{
		return type;
	}

	public void setName(String name)
	{
		this.name = name;
		notifyModifiedEvent();
	}

	private void notifyModifiedEvent()
	{
		for(ExplorerNodeModifyListener listener : explorerNodeListeners)
		{
			listener.modified(this);
		}
	}

	public void setOwner(String owner)
	{
		owner = owner;
	}

	public void setPath(String path)
	{
		this.path = path;
	}

	public void setPermission(int permission)
	{
		this.permission = permission;
	}

	public void setSize(long size)
	{
		this.size = size;
	}

	public void setType(String type)
	{
		this.type = type;
	}

	public void addExplorerNodeModifyListener(ExplorerNodeModifyListener listener)
	{
		if(explorerNodeListeners == null)
			explorerNodeListeners = new ArrayList<ExplorerNodeModifyListener>();
		explorerNodeListeners.add(listener);
	}
	
	@Override
	public boolean equals(Object arg0)
	{
		if (arg0 instanceof Contents)
		{
			Contents con = ((Contents)arg0);
			if(con.getName().equals(name) && con.getPath().equals(path) && con.getType().equals(type))
				return true;
		}
		return false;
	}

	public ExplorerNode getExplorerNode(int index)
	{
		return null;
	}
	
	@Override
	public String toString()
	{
		return name + "." + type;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}
	
	public int getUid() {
		return uid;
	}
}
